package com.lyw.utils;

import com.lyw.domain.GlobalConfigurationInfo;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 窗口工具类
 */
public class WindowUtil {

  public static BufferedImage getFile(Robot robot, WinDef.RECT rect) {
    return robot.createScreenCapture(rect.toRectangle());
  }

  public static WinDef.RECT getWindowPic(GlobalConfigurationInfo configInfo) {
    return getWindowPic(configInfo.getGameName());
  }

  public static WinDef.RECT getWindowPic(String name) {

    WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, name);
    WinDef.RECT rect = new WinDef.RECT();

    User32.INSTANCE.ShowWindow(hwnd, WinUser.SW_RESTORE);
    User32.INSTANCE.GetWindowRect(hwnd, rect);

    if (rect.toRectangle().width == 0) {
      throw new RuntimeException("请不要关闭或者隐藏！");
    }
    return rect;
  }
}
